function output = JoinTfcPeaksOverSubjects()

[OutputPrefix Files Outfiles] = GetConfigurationCWDJDSJF();

output.datatable = [];
fData = []; fPeaks = [];
for fI = 1:length(Files)
    
    tfc = JoinTfcSet(Files{fI}, OutputPrefix);
    save(Outfiles{fI}, 'tfc', '-v7.3');
%     load(Outfiles{fI});
    
    cfgPTD = plotTfcData_defaultCfg();
    cfgPTD.Smoothing.Width = 10;
    cfgPTD.PlotPerRegion = false;
    cfgPTD.SaveFigure = false;
    figsD = plotTfcData_Core(tfc, [], cfgPTD);
    fData = [fData figsD]; % Add all plots to the list of figures
    
    [datatable header] = TfcToDatatable(tfc);
    if isempty(output.datatable), output.datatable = datatable;
    else output.datatable = [output.datatable; datatable];
    end
    
    cfgPTP = plotTfcPeaks_defaultCfg();
    fPeaks(end+1) = plotTfcPeaks_Core(datatable, header, OutputPrefix, tfc.epochs, cfgPTP);
end

output.header = header;

for i = 1:length(fData)
    saveFigure(fData(i), [getpref('ECoG','Results') 'TFC\Joined\'], [OutputPrefix ' Tfc Data ' num2str(i)]);
    close(fData(i));
end

for i = 1:length(fPeaks)
    saveFigure(fPeaks(i), [getpref('ECoG','Results') 'TFC\Joined\'], [OutputPrefix ' Tfc Peaks ' num2str(i)]);
    close(fPeaks(i));
end

end


function tfc = JoinTfcSet(Files, subjectname)


for i = 1:length(Files)
    DATA{i} = load(Files{i});
end
D = DATA{1};

tfc.freqbindescriptions = D.tfc.freqbindescriptions;
tfc.freqbins = D.tfc.freqbins;
tfc.times = D.tfc.times;
tfc.epochs = D.tfc.epochs;
tfc.subject = subjectname;
channels = fieldnames(D.tfc.data);

diary('Result of Join.txt');
disp('--------------------------------------------------------------------');
disp(['Files used for: Joined ' tfc.epochs]);
for i = 1:length(Files)
    disp([' -- ' Files{i} '; ' DATA{i}.tfc.subject]);
end
diary off;

for i = 1:length(channels) % For every subregion
    disp(channels{i});
    
    
    for frq = 1:length(D.tfc.freqbindescriptions) % For every frequencybin
        tfc.data.(channels{i})(frq).frequencyrange = D.tfc.freqbins(frq,:);
        if length(DATA)>1
            m = [];
            %             s = [];
            joinCount = 0;
            for k = 1:length(DATA) % for all DATA's
                if any(strcmp(fieldnames(DATA{k}.tfc.data), channels{i}))
                    joinCount = joinCount + 1;
                    m(:,joinCount) = DATA{k}.tfc.data.(channels{i})(frq).mean;
                    %                 s(:,k) = DATA{k}.tfc.data.(channels{i})(frq).std;
                end
            end
            tfc.data.(channels{i})(frq).mean = mean(m,2);
            %             tfc.data.(channels{i})(frq).std = sqrt(sum(s.^2, 2));
            tfc.data.(channels{i})(frq).std = std(m,0,2);
        else
            error('Next few lines are wrong, fix first!');
            tfc.data.(channels{i})(frq).mean = D.tfc.data.(channels{i})(frq).mean;
            tfc.data.(channels{i})(frq).std = D.tfc.data.(channels{i})(frq).std;
        end
        PEAKS = scorePeaks(tfc.data.(channels{i})(frq), tfc.times, num2str(joinCount));
        tfc.data.(channels{i})(frq).peaklocs = PEAKS.peaklocs;
        tfc.data.(channels{i})(frq).peaktype = PEAKS.peaktype;
        tfc.data.(channels{i})(frq).firstEffect = PEAKS.firstEffect;
        tfc.data.(channels{i})(frq).pvalue = PEAKS.pvalue;
    end
end

end

function [OutputPrefix Files Outfiles] = GetConfigurationCWDJDSJF()
OutputPrefix = 'Joined_CW_DJ_DS_JF';
Files{1} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Ambiguous Face Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Ambiguous Face Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Ambiguous Face Response Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Ambiguous Face Response Tfc.mat'
    };
Outfiles{1} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Ambiguous Face Response Tfc.mat'];

Files{2} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Ambiguous House Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Ambiguous House Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Ambiguous House Response Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Ambiguous House Response Tfc.mat'
    };
Outfiles{2} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Ambiguous House Response Tfc.mat'];

Files{3} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous Face Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous Face Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous Face Response Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous Face Response Tfc.mat' 
    };
Outfiles{3} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous Face Response Tfc.mat'];

Files{4} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous House Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous House Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous House Response Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous House Response Tfc.mat'
    };
Outfiles{4} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous House Response Tfc.mat'];

Files{5} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous Face Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous Face Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous Face Stimulus Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous Face Stimulus Tfc.mat' 
    };
Outfiles{5} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous Face Stimulus Tfc.mat'];

Files{6} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous House Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous House Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous House Stimulus Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous House Stimulus Tfc.mat'
    };
Outfiles{6} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous House Stimulus Tfc.mat'];

Files{7} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW Sphere Ambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ Sphere Ambiguous Response Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegFG Sphere Ambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegJF Sphere Ambiguous Response Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV Sphere Ambiguous Response Tfc.mat' 
    };
Outfiles{7} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' Sphere Ambiguous Response Tfc.mat'];

Files{8} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW Sphere Unambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ Sphere Unambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegJF Sphere Unambiguous Response Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV Sphere Unambiguous Response Tfc.mat' 
    };
Outfiles{8} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' Sphere Unambiguous Response Tfc.mat'];

Files{9} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW Sphere Unambiguous Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ Sphere Unambiguous Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegJF Sphere Unambiguous Stimulus Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV Sphere Unambiguous Stimulus Tfc.mat' 
    };
Outfiles{9} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' Sphere Unambiguous Stimulus Tfc.mat'];

%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Files{10} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Ambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Ambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Ambiguous Response Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Ambiguous Face Response Tfc.mat'
    };
Outfiles{10} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Ambiguous Response Tfc.mat'];
Files{11} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous Response Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous Face Response Tfc.mat' 
    };
Outfiles{11} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous Response Tfc.mat'];
Files{12} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous Stimulus Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous Face Stimulus Tfc.mat' 
    };
Outfiles{12} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous Stimulus Tfc.mat'];
end

function [OutputPrefix Files Outfiles] = GetConfigurationCWDJDSJFRV()
OutputPrefix = 'Joined_CW_DJ_DS_JF_RV';
Files{1} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Ambiguous Face Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Ambiguous Face Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Ambiguous Face Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Ambiguous Face Response Tfc.mat', ...
    };
Outfiles{1} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Ambiguous Face Response Tfc.mat'];

Files{2} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Ambiguous House Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Ambiguous House Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Ambiguous House Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Ambiguous House Response Tfc.mat'
    };
Outfiles{2} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Ambiguous House Response Tfc.mat'];

Files{3} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous Face Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous Face Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous Face Response Tfc.mat', ... 
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous Face Response Tfc.mat' 
    };
Outfiles{3} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous Face Response Tfc.mat'];

Files{4} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous House Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous House Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous House Response Tfc.mat', ... 
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous House Response Tfc.mat'
    };
Outfiles{4} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous House Response Tfc.mat'];

Files{5} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous Face Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous Face Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous Face Stimulus Tfc.mat', ... 
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous Face Stimulus Tfc.mat' 
    };
Outfiles{5} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous Face Stimulus Tfc.mat'];

Files{6} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous House Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous House Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous House Stimulus Tfc.mat', ... 
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous House Stimulus Tfc.mat'
    };
Outfiles{6} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous House Stimulus Tfc.mat'];

Files{7} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW Sphere Ambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ Sphere Ambiguous Response Tfc.mat', ... %'D:\ECoG\Data\ByRegion\Timefreq\RegFG Sphere Ambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegJF Sphere Ambiguous Response Tfc.mat', ... 
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV Sphere Ambiguous Response Tfc.mat' 
    };
Outfiles{7} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' Sphere Ambiguous Response Tfc.mat'];

Files{8} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW Sphere Unambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ Sphere Unambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegJF Sphere Unambiguous Response Tfc.mat', ... 
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV Sphere Unambiguous Response Tfc.mat' 
    };
Outfiles{8} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' Sphere Unambiguous Response Tfc.mat'];

Files{9} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW Sphere Unambiguous Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ Sphere Unambiguous Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegJF Sphere Unambiguous Stimulus Tfc.mat', ... 
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV Sphere Unambiguous Stimulus Tfc.mat' 
    };
Outfiles{9} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' Sphere Unambiguous Stimulus Tfc.mat'];
%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Files{10} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Ambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Ambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Ambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Ambiguous Response Tfc.mat', ...
    };
Outfiles{10} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Ambiguous Response Tfc.mat'];
Files{11} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous Response Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous Response Tfc.mat', ... 
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous Response Tfc.mat' 
    };
Outfiles{11} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous Response Tfc.mat'];
Files{12} = {...
    'D:\ECoG\Data\ByRegion\Timefreq\RegCW FaceHouse Unambiguous Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDJ FaceHouse Unambiguous Stimulus Tfc.mat', ...
    'D:\ECoG\Data\ByRegion\Timefreq\RegDS FaceHouse Unambiguous Stimulus Tfc.mat', ... 
    'D:\ECoG\Data\ByRegion\Timefreq\RegRV FaceHouse Unambiguous Stimulus Tfc.mat' 
    };
Outfiles{12} = ['D:\ECoG\Data\ByRegion\Timefreq\' OutputPrefix ' FaceHouse Unambiguous Stimulus Tfc.mat'];

end